<?php

namespace App\Service\Collect;


use App\Service\BaiduNlp;
use Illuminate\Support\Facades\Cache;
use QL\QueryList;

class Xueqiu extends Base
{
    // 从雪球采集
    //   采集接口用curl不成功，
    //     QueryList与GuzzleHttp也不成功
    //   只有file_get_contents执行成功

    // 初始化
    protected function init()
    {
        parent::init();
    }


    // 保存到数据表
    public function saveApiDataToDb($_item, $key, $is_file = false)
    {
        // 执行采集
        $_content = $_item['text'];
        $_title   = $this->removeHtml($_item['title']);
        $_relay   = $_item['retweet_count'];
        $_comment = $_item['reply_count'];
        $_praise  = $_item['like_count'];
        $_read_no = $_item['view_count'];
        $_images  = $_item['pic'];  // 去掉 !thumb.jpg
        $_cover   = $_item['firstImg'] ?? '';  // !thumb.jpg
        $_id      = $_item['id'];
        $_ptime   = $_item['created_at'] / 1000;
        $_author  = $_item['user']['screen_name'];
        $_desc    = $this->removeHtml($_item['description'], '');
        $_date    = $_item['timeBefore'];  // 2019-06-20 09:43

        // 采集地址
        $_href  = 'https://xueqiu.com'.$_item['target'];
        $_ctime = $_ptime ?: strtotime($_date); // 时间
        if($_cover){
            $_cover = substr($_cover, 0, -10);
        }
        if($_images){
            $_images = explode(',', $_images);
            $images  = [];
            foreach($_images as $_img){
                $images[] = substr($_img, 0, -10);
            }
            $_images = $images;
        }

        // 只采集有评论的
        /*if($_comment < 1){
            return $this->return_false(' '.($key + 1).'：==> 文章：评论数过少，不采集：'.$_title);
        }*/


        // 判断链接是否已经采集
        if($is_file){
            $info = $this->isGatherFile($_href);
        } else{
            $info = $this->isGather($_href);
        }
        if($info){
            if(empty($info['sentiment'])){
                $sentiment = $this->baiduNlp($info['text'] ?: $info['description']);
                if($sentiment){
                    if($is_file){
                        $info['sentiment'] = $sentiment;
                        $this->updateFile($_href, $info);
                    } else{
                        $this->updateArticle($info['id'], ['sentiment' => $sentiment]);
                    }
                }
            }

            return $this->return_false(' '.($key + 1).'：==> 已经采集文章：'.$_title);
        }

        // 只采集今天的
        if($_ctime < $this->startTime){
            return $this->return_die(' '.($key + 1).'：==> 文章：不是今天的文章：'.$_title);
        }
        // 如果没有标题
        /*if(empty($_title)){
            return $this->return_false(' '.($key + 1).'：==> 文章：没有标题，不能采集：'.$_title);
        }*/

        // 全匹配 精准匹配
        if((strpos($_desc, $this->keyword) === false) && strpos($_title, $this->keyword) === false){
            return $this->return_false('==> 文章：文章没有精准匹配到搜索的关键字！');
        }
        // 排除关键字 宋正兴
        if(($this->keyword == '董事长宋正') && (strpos($_desc, '宋正兴') !== false) || strpos($_title, '宋正兴') !== false){
            return $this->return_false('==> 文章：文章匹配到其他关键词！');
        }

        // 判断 情感倾向
        //  "sentiment":2,   // 表示情感极性分类结果，0:负向，1:中性，2:正向
        /* $sentiment = '';  // 默认
        $full      = [0 => '负向', 1 => '中性', 2 => '正向'];
        $result    = (new BaiduNlp())->sentiment($_desc);
        if(isset($result['items'][0]['sentiment'])){
            $sentiment = $full[$result['items'][0]['sentiment']] ?? $sentiment;
        } */
        $sentiment = $this->baiduNlp($_desc);

        $_channel  = '讨论';
        $from_site = '雪球';
        $site_id   = 1;
        $_row      = [
            'channel'      => 'thread',
            'sentiment'    => $sentiment,
            'link'         => $_href,
            'title'        => $_title,
            'description'  => $_desc,
            'text'         => $_content,
            'time'         => $_ctime,
            'cover'        => $_cover,
            'images'       => $_images,
            'author'       => trim($_author),
            'from_channel' => $_channel,
            'from_site'    => $from_site,
            'site_id'      => $site_id,
            'read_no'      => intval($_read_no),
            'relay_no'     => intval($_relay),
            'praise_no'    => intval($_praise),
            'comment_no'   => intval($_comment),
        ];

        //var_dump($_row);
        if($is_file){
            $res = $this->saveFile($_row);
        } else{
            $res = $this->saveArticle($_row);
        }


        return $res;
    }


    // 获取api接口的列表(测试)
    //   采集接口用curl需要 Cookie
    public function getApiList($keyword, $page = 1, $size = 10)
    {
        if(empty($keyword)){
            return $this->return_die('传递采集关键字！');
        }

        // 使用 curl header
        try{
            $url    = 'https://xueqiu.com/query/v1/search/status.json';
            $post   = $this->makeApiParam($keyword, $page, $size);
            $cookie = $this->getCookie();
            $header = $this->getHeader($cookie);
            $res    = $this->apiCurl()->header($header)->get($post)->request($url);
        } catch(\Exception $e){
            return $this->return_false($e->getMessage());
        }


        // 没有cookie的返回数据
        // string(44) "{"code":400016,"message":"","success":false}"
        $json = json_decode($res, true);
        if(!empty($json['list'])){
            return $json['list'];
        }

        // 使用 curl cookie
        // 重新获取使用
        try{
            $cookie = $this->getCookie(true);
            $header = $this->getHeader($cookie);
            $res    = $this->apiCurl()->header($header)->get($post)->request($url);
            $json   = json_decode($res, true);
        } catch(\Exception $e){
            return $this->return_false($e->getMessage());
        }
        if(!empty($json['list'])){
            return $json['list'];
        }

        return $this->return_die('    雪球：没有采集内容：'.$keyword.'   页数：'.$page.'  每页数量'.$size);
    }


    // 获取api接口的列表(测试)
    //   采集接口用curl需要 Cookie  在header中使用（执行成功）
    public function getApiListCookie($keyword, $page = 1, $size = 10)
    {
        if(empty($keyword)){
            return $this->return_die('传递采集关键字！');
        }

        $url  = 'https://xueqiu.com/query/v1/search/status.json';
        $post = $this->makeApiParam($keyword, $page, $size);

        // 使用 curl cookie
        // 没有cookie的返回数据
        // string(44) "{"code":400016,"message":"","success":false}"
        try{
            $cookie = $this->getCookie();
            $res    = $this->apiCurl()->cookie($cookie)->get($post)->request($url);
            $json   = json_decode($res, true);
        } catch(\Exception $e){
            return $this->return_false($e->getMessage());
        }
        if(!empty($json['list'])){
            return $json['list'];
        }

        // 重新获取使用
        try{
            $cookie = $this->getCookie(true);
            $res    = $this->apiCurl()->cookie($cookie)->get($post)->request($url);
            $json   = json_decode($res, true);
        } catch(\Exception $e){
            return $this->return_false($e->getMessage());
        }
        if(!empty($json['list'])){
            return $json['list'];
        }

        return [];
    }


    // 获取 cookie
    protected function getCookie($isForce = false)
    {
        $cookie = '';
        if(empty($isForce)){
            $cookie = Cache::get('cookie.xueqiu');
        }
        if(empty($cookie)){
            $url = 'https://xueqiu.com/k?q=易见股份#/timeline';
            $url = 'https://xueqiu.com/k';
            $res = $this->apiCurl()->returnHeader(true)->request($url);
            // 制作缓存
            $cookies = $res['cookies'];
            foreach($cookies as $_cookie){
                $cookie .= $_cookie.';';
            }

            Cache::set('cookie.xueqiu', $cookie, 6 * 3600);
        }

        return $cookie;
    }


    // header参数（cookie是必传的）
    protected function getHeader($cookie)
    {
        return [
            'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'Accept-Encoding: deflate, br',
            'Accept-Language:zh-CN,zh;q=0.9',
            'Cache-Control:max-age=0',
            'Connection:keep-alive',
            'Host:xueqiu.com',
            'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',
            'Cookie:'.$cookie,
            //'Cookie:cookiesu=351707027255538; u=351707027255538; device_id=fec6b3186474d47d18403cf1b11a28d4; xq_a_token=76d4e5ee97f60e0be2c9b6c094156d577fba5c5b; xqat=76d4e5ee97f60e0be2c9b6c094156d577fba5c5b; xq_r_token=2537d01490f74c00d7d4a37578ac84f1b7481ca1; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTcxMTA2ODM4MiwiY3RtIjoxNzA5MjYyMzI0NTE2LCJjaWQiOiJkOWQwbjRBWnVwIn0.kCkjp9fQZBTLq5M8LpE0x4mlCMC0SVM6czTQbK5WCl3n8Y66YXmuxuKJlPbBBJEHsKStUkdk0Rd7VLnIrOYGC0aQ3QTirEgCe1wwT8Inq06zG_pkIOfUqNylDA3I1uNsarppIeKXA-pmhTKhlqHBPZxb2GFx8-umYcHo3Z-PdKv51KJqmFo5Y2sXsIMeaMlNbAOtslA32hNvB0Eq2K1sDd6pdZObyzVwT01TjxJbvl7-d75bt74wkNjQq37upYKN_cdY-9kOQx72kmRKlHQpFHwMWv94380wSzPFwsKzcWUO6j7-rNXdjhGHU4ayC4-JukLGxUjBFPsTGBAEBE8XLg; Hm_lvt_1db88642e346389874251b5a1eded6e3=1707027256,1709262381; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1709292360; acw_tc=276077a817096049634778787e7bad6b58bdeb3c3166371d6f45ea2901fc27',
        ];
    }


    // 采集地址的参数
    // https://xueqiu.com/query/v1/search/status.json?sortId=1&q=易见股份&count=20&page=1
    protected function makeApiParam($keyword, $page = 1, $size = 10)
    {
        return ["sortId" => "2", "q" => $keyword, "count" => $size, "page" => $page,];
    }

}

